package org.ofbiz.wvtools.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilValidate;

public class WvDatetimeUtils {

	 /**
     * 获取今天时间
     * @return
     */
	public static Timestamp getToDayTimestamp(){
		//本日 ，本周，本月 ，本年
		Date date = new Date();
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		// 将时分秒,毫秒域清零
		cal1.set(Calendar.HOUR_OF_DAY, 0);
		cal1.set(Calendar.MINUTE, 0);
		cal1.set(Calendar.SECOND, 0);
		cal1.add(Calendar.DATE, 0);
		date = cal1.getTime();
		
		Timestamp thruDate = new Timestamp(date.getTime());
		return thruDate;
	}
	 /**
     * 获取今天最晚时间
     */
	public static Timestamp getToDayLastTime(){
 	   //本日 ，本周，本月 ，上月，本年
 		Date date = new Date();
 		Calendar cal1 = Calendar.getInstance();
 		cal1.setTime(date);
 		// 将时分秒,毫秒域清零
 		cal1.set(Calendar.HOUR_OF_DAY, 0);
 		cal1.set(Calendar.MINUTE, 0);
 		cal1.set(Calendar.SECOND, 0);
 		cal1.add(Calendar.DATE, 1);
 		date = cal1.getTime();
 		// 查询当日应该是今天到明天为止
 		Timestamp thruDate = new Timestamp(date.getTime());
 		Debug.log("thruDate=" + thruDate);
 		return thruDate; 
    }
	
    /** 
     * 获取上几天
     */  
    public static Timestamp getPreDay(Timestamp date , Long preDayNum) {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);
        cal.add(Calendar.DATE, (int)(preDayNum*1));  
 		cal.set(Calendar.HOUR_OF_DAY, 0);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);

        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String lastMonth = dft.format(cal.getTime());  
        Timestamp newTime = Timestamp.valueOf(lastMonth);
        return newTime;  
    } 
    
    public static void main(String[] args) {
	Timestamp thruDate = WvDatetimeUtils.getToDayTimestamp();
	Timestamp fromDate = WvDatetimeUtils.getLastDay(thruDate, new Long(-1));
	Timestamp fromDate_chengmo = WvDatetimeUtils.getLastDay(thruDate, new Long(-90));
	System.out.println(fromDate_chengmo);
    }
   
   
	 /**
     * 获取上几天
     * 
     * @return
     */
    public static Timestamp getLastDay(Timestamp date, Long day) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	cal.add(Calendar.DATE, (int) (day * 1));
	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String lastMonth = dft.format(cal.getTime());
	Timestamp newTime = Timestamp.valueOf(lastMonth);
	return newTime;
    }
    public static Timestamp getEndDate(Timestamp date) {
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    	cal.set(Calendar.DAY_OF_MONTH , maxDay);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    	String lastMonth = dft.format(cal.getTime());
    	Timestamp newTime = Timestamp.valueOf(lastMonth);
		return newTime;
	}

    public static Long getNowMonth(Timestamp date) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	String month = new SimpleDateFormat("MM").format(cal.getTime());
	return Long.valueOf(month);
    }

    public static Long getNowYear(Timestamp date) {
	Calendar cal = Calendar.getInstance();
	cal.setTime(date);
	String year = new SimpleDateFormat("yyyy").format(cal.getTime());
	return Long.valueOf(year);
    }

    public static String getCurrentYear() {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
	Date date = new Date();
	return sdf.format(date);
    }
    
    
    
    private static String pattern="yyyy-MM";
	private static SimpleDateFormat sdf = new SimpleDateFormat(pattern);
	public static  boolean isNextMonth(Date date) {
		if(UtilValidate.isEmpty(date)) {
			 return false;
		}
		// 设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfMonth = calendar1.get(Calendar.DAY_OF_MONTH) - 1;
        int offset1 = 1 - dayOfMonth;
        int offset2 = 30 - dayOfMonth;
        calendar1.add(Calendar.DATE, offset1 + 30);
        calendar2.add(Calendar.DATE, offset2 + 30);
        // 下月一号
        String day1 = sdf.format(calendar1.getTime());
        System.out.println(day1);
        // 下月月末
        String day2 = sdf.format(calendar2.getTime());
        System.out.println(day2);

        String format = sdf.format(date);
        try {
            Date parse = sdf.parse(format);
            Date monthday1 = sdf.parse(day1);
            Date monthday2 = sdf.parse(day2);
            return parse.compareTo(monthday1) >= 0 && monthday2.compareTo(parse) >= 0;
        } catch (ParseException e) {
            Debug.log(e.getMessage());
        }
        return false;
		
	}
	public static  boolean isNext2Month(Date date) {
		if(UtilValidate.isEmpty(date)) {
			 return false;
		}
		// 设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfMonth = calendar1.get(Calendar.DAY_OF_MONTH) - 1;
        int offset1 = 1 - dayOfMonth;
        int offset2 = 30 - dayOfMonth;
        calendar1.add(Calendar.DATE, offset1 + 60);
        calendar2.add(Calendar.DATE, offset2 + 61);
        // 下月一号
        String day1 = sdf.format(calendar1.getTime());
        System.out.println(day1);
        // 下月月末
        String day2 = sdf.format(calendar2.getTime());
        System.out.println(day2);

        String format = sdf.format(date);
        try {
            Date parse = sdf.parse(format);
            Date monthday1 = sdf.parse(day1);
            Date monthday2 = sdf.parse(day2);
            return parse.compareTo(monthday1) >= 0 && monthday2.compareTo(parse) >= 0;
        } catch (ParseException e) {
            Debug.log(e.getMessage());
        }
        return false;
		
	}
	public static  boolean isNext3Month(Date date) {
		if(UtilValidate.isEmpty(date)) {
			 return false;
		}
		// 设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfMonth = calendar1.get(Calendar.DAY_OF_MONTH) - 1;
        int offset1 = 1 - dayOfMonth;
        int offset2 = 30 - dayOfMonth;
        calendar1.add(Calendar.DATE, offset1 + 91);
        calendar2.add(Calendar.DATE, offset2 + 181);
        // 下月一号
        String day1 = sdf.format(calendar1.getTime());
        System.out.println(day1);
        // 下月月末
        String day2 = sdf.format(calendar2.getTime());
        System.out.println(day2);

        String format = sdf.format(date);
        try {
            Date parse = sdf.parse(format);
            Date monthday1 = sdf.parse(day1);
            Date monthday2 = sdf.parse(day2);
            return parse.compareTo(monthday1) >= 0 && monthday2.compareTo(parse) >= 0;
        } catch (ParseException e) {
            Debug.log(e.getMessage());
        }
        return false;
		
	}
	
	//判断选择的日期是否是本月
    public static boolean isThisMonth(long time)
    {
    	 return isThisTime(time,"yyyy-MM");
    }
    private static boolean isThisTime(long time,String pattern) {
		Date date = new Date(time);
         SimpleDateFormat sdf = new SimpleDateFormat(pattern);
         String param = sdf.format(date);//参数时间
         String now = sdf.format(new Date());//当前时间
         if(param.equals(now)){
      	   return true;
         }
         return false;
	}
}
